import xtriui
import uix
import uiutil
import uiconst
import util
import _weakref
import weakref
import blue
import base
import uthread
import types
import uix
import listentry
import stackless
import lg
import html
import draw
import sys
import dbg
import uicls
import uiutil
import uiconst
SCROLLMARGIN = 0
MINCOLUMNWIDTH = 24

class Scroll(uicls.ScrollCore):
    __guid__ = 'uicls.Scroll'
    headerFontSize = 10

    def ApplyAttributes(self, attributes):
        uicls.ScrollCore.ApplyAttributes(self, attributes)
        sm.GetService('window').CheckControlAppearance(self)



    def Prepare_ScrollControls_(self):
        self.sr.scrollcontrols = uicls.ScrollControls(name='__scrollcontrols', parent=self.sr.maincontainer, align=uiconst.TORIGHT, pos=(0, 0, 10, 0), state=uiconst.UI_HIDDEN, idx=0)
        self.sr.scrollcontrols.Startup(self)



    def Prepare_Underlay_(self):
        uicls.ScrollCore.Prepare_Underlay_(self)
        self.ChangeUnderlay()



    def ChangeUnderlay(self):
        self.sr.underlay.texture.pixels = 'res:/UI/Texture/UITexLib_03.dds'
        self.sr.underlay.cornerSize = 6
        self.sr.underlay.rectTop = 64
        self.sr.underlay.rectWidth = 54
        self.sr.underlay.rectHeight = 54
        self.sr.underlay.pos = (0, 0, 0, 0)
        self.sr.underlay.left = 0
        self.sr.underlay.top = 0
        self.sr.underlay.width = 0
        self.sr.underlay.height = 0
        self.sr.underlay.tripass = eve.rot.GetInstance('res:/UI/Pass/wndBackground.blue')



    def Startup(self, minZ = None):
        return 



    def ShowHint(self, hint = None):
        if ((self.sr.hint is None) and hint):
            self.sr.hint = uix.GetCaption(hint, self.sr.clipper, align=uix.UI_ALCLIENT, left=16, top=32, autoHeight=0, autoWidth=0)
        elif ((self.sr.hint is not None) and hint):
            self.sr.hint.text = hint
            self.sr.hint.state = uix.UI_DISABLED
        elif ((self.sr.hint is not None) and (not hint)):
            self.sr.hint.state = uix.UI_HIDDEN



    def HideBackground(self, alwaysHidden = 0):
        frame = None
        if uiutil.GetAttrs(self, 'sr', 'underlay'):
            self.sr.underlay.state = uiconst.UI_HIDDEN
            frame = self.sr.underlay
        if (frame and getattr(frame, 'parent')):
            underlayFrame = frame.parent.FindChild('underlayFrame')
            underlayFill = frame.parent.FindChild('underlayFill')
            if underlayFrame:
                underlayFrame.state = uiconst.UI_HIDDEN
            if underlayFill:
                underlayFill.state = uiconst.UI_HIDDEN
        if alwaysHidden:
            self.SetNoBackgroundFlag(alwaysHidden)



    def _OnMouseWheel(self, *etc):
        if getattr(self, 'wheeling', 0):
            return 1
        else:
            if len(uicore.layer.dragging.children):
                return 1
            if len(uicore.layer.menu.children):
                focus = uicore.registry.GetFocus()
                if (focus and isinstance(focus, uicls.ScrollCore)):
                    if (focus.name not in ('combodropdown', '_comboDropDown')):
                        return 1
            self.wheeling = 1
            self.Scroll((eve.triapp.uilib.dz / 240.0))
            self.wheeling = 0
            return 1



    def GetNoItemNode(self, text, sublevel = 0, *args):
        return listentry.Get('Generic', {'label': text,
         'sublevel': sublevel})



    def RecyclePanel(self, panel, fromWhere = None):
        if (panel.__guid__ == 'listentry.VirtualContainerRow'):
            subnodes = [ node for node in panel.sr.node.internalNodes if (node is not None) ]
            for node in subnodes:
                node.panel = None

        uicls.ScrollCore.RecyclePanel(self, panel, fromWhere)




class ScrollControls(uicls.ScrollControlsCore):
    __guid__ = 'uicls.ScrollControls'

    def ApplyAttributes(self, attributes):
        uicls.ScrollControlsCore.ApplyAttributes(self, attributes)



    def Prepare_(self):
        uicls.Line(parent=self, align=uiconst.TOLEFT, color=(1.0, 1.0, 1.0, 0.06))
        self.Prepare_ScrollHandle_()
        self.Prepare_ScrollButtons_()



    def Prepare_ScrollButtons_(self):
        btn = uicls.ScrollBtn(name='scrollBtnTop', parent=self, width=10, height=12, align=uiconst.CENTERTOP, state=uiconst.UI_NORMAL)
        self.sr.scrollUpBtn = btn
        icon = uicls.Icon(icon='ui_38_16_105', align=uiconst.CENTER, parent=btn, state=uiconst.UI_DISABLED, pos=(1, 0, 0, 0))
        btn = uicls.ScrollBtn(name='scrollBtnBottom', parent=self, width=10, height=12, align=uiconst.CENTERBOTTOM, state=uiconst.UI_NORMAL)
        self.sr.scrollDownBtn = btn
        icon = uicls.Icon(icon='ui_38_16_104', align=uiconst.CENTER, parent=btn, state=uiconst.UI_DISABLED, pos=(1, 0, 0, 0))



    def Prepare_ScrollHandle_(self):
        subparent = uicls.Container(name='subparent', parent=self, align=uiconst.TOALL, padding=(-1, 8, 0, 8))
        self.sr.scrollhandle = uicls.ScrollHandle(name='__scrollhandle', parent=subparent, align=uiconst.TOPLEFT, pos=(0, 0, 0, 0), state=uiconst.UI_NORMAL, idx=0)



    def Startup(self, dad):
        self.sr.dad = dad
        self.sr.scrollhandle.Startup(dad)
        self.sr.scrollUpBtn.Startup(dad, 1)
        self.sr.scrollDownBtn.Startup(dad, -1)




class ScrollHandle(uicls.ScrollHandleCore):
    __guid__ = 'uicls.ScrollHandle'

    def Prepare_(self):
        dotFrame = uicls.Frame(name='dotFrame', parent=self, frameConst=('ui_38_16_102', 5, -2))
        dotFrame.tripass = blue.os.LoadObject('res:/UI/Pass/dot.blue')
        shapeFrame = uicls.Frame(name='shapeFrame', parent=self, frameConst=('ui_38_16_103', 5, -2), color=(1.0, 1.0, 1.0, 1.0))
        self.Prepare_Hilite_()



    def Prepare_Hilite_(self):
        self.sr.hilite = uicls.Fill(parent=self, color=(1.0, 1.0, 1.0, 0.3), pos=(2, 1, 1, 1), state=uiconst.UI_HIDDEN)




class ColumnHeader(uicls.ScrollColumnHeaderCore):
    __guid__ = 'uicls.ScrollColumnHeader'
    letterspace = 2


class DottedLine(xtriui.SlimDeco):
    __guid__ = 'xtriui.DottedLine'
    __nonpersistvars__ = []

    def init(self):
        return 



    def Load(self, dot = 1, gap = 3):
        self.dot = dot
        self.gap = gap
        self.Refresh()



    def OnResize(self, *args):
        return 



    def Refresh(self):
        t = self.control.model.areas[0].areaTextures[0]
        t.pixels = ('res:/UI/Texture/dotline_%s_%s.dds' % (self.dot, self.gap))
        t.scaling.x = float((self.width / (self.dot + self.gap)))
        t.scaling.y = float((self.height / (self.dot + self.gap)))




